package br.com.bolsamovel;

import java.text.NumberFormat;

import org.droidsql.DAO;
import org.droidsql.DBHelpper;

import br.com.bolsamovel.db.entities.Account;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;

public class OrdersHistoryActivity extends Activity {
	private TextView txtHeader;
	private ListView lstOrders;
	private SimpleCursorAdapter orderCursorAdapter;

	private NumberFormat decFormat = NumberFormat.getCurrencyInstance();

	@Override
	protected void onResume() {
		super.onResume();
		onShow();
	}

	private void onShow() {
		try {
			Intent i = getIntent();

			int accountId = i.getIntExtra("account_id", -1);

			DAO<Account> dao = new DAO<Account>(Account.class);

			Account acc = dao.findByID(String.valueOf(accountId));

			txtHeader.setText(acc.name);

			String stockId = i.getStringExtra("stock_id");

			loadOrders(accountId, stockId);
		} catch (Exception e) {
			UIUtils.showErrorMessage(this, e);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.orders_history_main);

		txtHeader = (TextView) findViewById(R.order_history.lbl_header);
		lstOrders = (ListView) findViewById(R.order_history.lst_view);

		orderCursorAdapter = new SimpleCursorAdapter(this, R.layout.orders_history_lst_item, null, new String[] { "stock_id", "order_date", "signal", "quantity", "price" }, new int[] { R.order_history.tv_papel, R.order_history.tv_data, R.order_history.tv_sinal, R.order_history.tv_qtd, R.order_history.tv_preco });

		orderCursorAdapter.setViewBinder(new OrdersListViewBinder());

		lstOrders.setAdapter(orderCursorAdapter);
	}

	protected void loadOrders(int accountId, String stockId) {
		StringBuffer sql = new StringBuffer();

		sql.append(" SELECT");
		sql.append(" 1 as _id,"); // o ListView precisa de uma coluna por nome
									// '_id'
		sql.append(" stock_id,");
		sql.append(" order_date,");
		sql.append(" signal,");
		sql.append(" quantity,");
		sql.append(" price");
		sql.append(" from stock_orders");
		sql.append(" where");
		sql.append(" account_id = ?");

		if (stockId != null) {
			sql.append(" and stock_id = '").append(stockId).append("'");
		}

		sql.append(" order by order_date desc, stock_id");

		Cursor c = DBHelpper.query(sql.toString(), new String[] { String.valueOf(accountId) });

		orderCursorAdapter.changeCursor(c);
		orderCursorAdapter.notifyDataSetChanged();
	}

	private class OrdersListViewBinder implements ViewBinder {

		public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
			String colName = cursor.getColumnName(columnIndex);

			if ("price".equals(colName)) {
				double value = cursor.getDouble(columnIndex);
				TextView tv = (TextView) view;

				tv.setText(decFormat.format(value));
			} else if ("signal".equals(colName)) {
				TextView tv = (TextView) view;

				int signalColor = Color.WHITE;

				if (cursor.getDouble(columnIndex) > 0) {
					signalColor = Color.GREEN;
					tv.setText("C");
				} else {
					signalColor = Color.RED;
					tv.setText("V");
				}
				tv.setTextColor(signalColor);
			} else if ("order_date".equals(colName)) {
				int data = cursor.getInt(columnIndex);

				TextView tv = (TextView) view;

				tv.setText(DateUtils.formatAsDate(data));
			} else {
				TextView tv = (TextView) view;
				tv.setText(cursor.getString(columnIndex));
			}
			return true;
		}
	}

}